package cadtoolbox.graphical;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Paint;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
import javax.swing.border.LineBorder;

import cadtoolbox.model.OligoGraph;
import cadtoolbox.model.SequenceVertex;

import org.apache.commons.collections15.Factory;

import edu.uci.ics.jung.algorithms.layout.CircleLayout;
import edu.uci.ics.jung.algorithms.layout.FRLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.SparseMultigraph;
import edu.uci.ics.jung.graph.util.EdgeType;
import edu.uci.ics.jung.visualization.*;
import edu.uci.ics.jung.visualization.control.EditingModalGraphMouse;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;


public class GraphicTest {
	
	public static void main(String[] args){
		
	
	final OligoGraph<SequenceVertex, String> g3 = new OligoGraph<SequenceVertex,String>();
	 g3.initFactories(new VertexFactory<SequenceVertex>(g3){
	    	
			public SequenceVertex create() {
				SequenceVertex newvertex = associatedGraph.popAvailableVertex();
				if (newvertex == null){
					newvertex = new SequenceVertex(associatedGraph.getVertexCount() + 1);
				} else {
					newvertex = new SequenceVertex(newvertex.ID);
				}
				return newvertex;
			} 	
			
			@Override
			public SequenceVertex copy(SequenceVertex original) {
				return new SequenceVertex(original.ID);
			} 
	    }, new EdgeFactory<SequenceVertex,String>(g3){
	    	public String createEdge(SequenceVertex v1, SequenceVertex v2){
	    		return v1.ID+"->"+v2.ID;
	    	}
	    	
	    	public String inhibitorName(String s){
	    		return "Inh"+s;
	    	}
	    });
    g3.saturablePoly = false;
//    SequenceVertex seq1 = new SequenceVertex(1);
//    SequenceVertex seq2 = new SequenceVertex(2);
//    SequenceVertex seq3 = new SequenceVertex(3);
//    g3.addVertex(seq1);
//    g3.addVertex(seq2);
//    g3.addVertex(seq3);
//    
//    g3.addActivation("1->1",seq1, seq1);
//    g3.addActivation("1->2", seq1, seq2);
//    g3.addActivation("2->Inh(1,1)", seq2, seq3);
//    g3.addInhibition("1->1", seq3, "1->1");
    
    
    DataPanel data = new DataPanel<String>(g3);
    GraphicInterfaceMouseAdapter myadapter = new GraphicInterfaceMouseAdapter();
    
    Layout<SequenceVertex, String> layout = new FRLayout(g3);
    layout.setSize(new Dimension(300,300)); // sets the initial size of the space
     // The BasicVisualizationServer<V,E> is parameterized by the edge types
     MyVisualizationServer<SequenceVertex,String> vv = 
              new MyVisualizationServer<SequenceVertex,String>(layout, myadapter,data);
     vv.setRenderer(new OligoRenderer<SequenceVertex,String>());
     vv.setPreferredSize(new Dimension(300,300));
     vv.setBorder(LineBorder.createBlackLineBorder());
     vv.addMouseListener(myadapter);
     vv.addMouseMotionListener(myadapter);
     vv.getRenderContext().setVertexFillPaintTransformer(new MyVertexFillPaintTransformer(g3));
     vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<SequenceVertex>());
     
     //JScrollPane scroll = new JScrollPane(vv);
     
     MyActionlistener<SequenceVertex,String> listen = new MyActionlistener<SequenceVertex,String>(layout,data);
     JButton newseq = new JButton("New Sequence");
     newseq.setActionCommand("newseq");
     newseq.addActionListener(listen);
     
     JButton duplicate = new JButton("duplicate");
     duplicate.setActionCommand("duplicate");
     duplicate.addActionListener(listen);
     
     JButton remove = new JButton("remove");
     remove.setActionCommand("remove");
     remove.addActionListener(listen);
     JButton fusion = new JButton("fusion");
     fusion.setActionCommand("fusion");
     fusion.addActionListener(listen);
     
     JButton save = new JButton("save");
     save.setActionCommand("save");
     save.addActionListener(listen);
     JButton load = new JButton("open");
     load.setActionCommand("open");
     load.addActionListener(listen);
     JButton addmodule = new JButton("add module");
     addmodule.setActionCommand("import");
     addmodule.addActionListener(listen);
     JButton export = new JButton("export");
     export.setActionCommand("export");
     export.addActionListener(listen);
     
     JButton plot = new JButton("plot");
     plot.setActionCommand("plot");
     plot.addActionListener(listen);
     
     JPanel actionpanel = new JPanel();
     actionpanel.setLayout(new GridLayout(4,2));
     actionpanel.add(newseq);
     actionpanel.add(duplicate);
     actionpanel.add(remove);
     actionpanel.add(fusion);
     actionpanel.add(save);
     actionpanel.add(load);
     actionpanel.add(addmodule);
     actionpanel.add(plot);
     actionpanel.setPreferredSize(new Dimension(150,150));
     
     JFrame frame = new JFrame("Simple Graph View");

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     GridBagLayout gbl = new GridBagLayout();
     GridBagConstraints gbc = new GridBagConstraints();
     frame.getContentPane().setLayout(gbl);
     gbc.fill = GridBagConstraints.BOTH;
     gbc.gridwidth = GridBagConstraints.REMAINDER;
     gbc.weightx = 1;
     gbc.weighty = 1;
    // gbl.setConstraints(scroll, gbc);
     gbl.setConstraints(vv, gbc);
     //frame.getContentPane().add(scroll);
 	 frame.getContentPane().add(vv);
     gbc.gridwidth = 1;
 	 gbc.gridheight = 2;
 	 gbc.weighty = 1;
 	 gbl.setConstraints(actionpanel, gbc);
 	 gbl.setConstraints(data, gbc);
     frame.getContentPane().add(actionpanel);
     frame.getContentPane().add(data);
     frame.pack();
     frame.setVisible(true);   
	}
	
}
